package ge.rules;


import ge.engine.PositionableArea;
import ge.engine.Rule;

public class ConfinamentAreaRule implements Rule,PositionableArea {

	private PositionableArea actor;
	private double x,y,w,h;
	
	public ConfinamentAreaRule()
	{
	}
	/**
	 * 	Sets the element who is going to be confined
	 * @param actor Confined actor
	 */
	public void setConfinedActor(PositionableArea actor)
	{
		this.actor = actor;
	}
	
	@Override
	public void setArea(double width, double height )
	{
		h = height;
		w = width;
	}
	/**
	 * 	You can create a new area from other one, taking
	 * 	size and actual position. But it's a new area, if
	 * 	the model area move, this wouldn't do the same.
	 * @param area Positionable Area
	 */
	public void setArea(PositionableArea area)
	{
		x = area.getX();
		y = area.getY();
		
		h = area.getWidth();
		w = area.getHeight();
	}
	/**
	 * @return Confined Actor
	 */
	public PositionableArea getConfinedActor() {
		return actor;
	}

	public double getWidth() {
		return w;
	}
	
	public double getHeight() {
		return h;
	}
	/**
	 * Update function
	 */
	@Override
	public void update() {
		if(actor.getX() < x+1)
			actor.setPos(x+1, actor.getY());
		else if(actor.getX()+actor.getWidth() > x+w)
			actor.setPos(x+w-actor.getWidth()-1, actor.getY());
		
		if(actor.getY() < y+1)
			actor.setPos(actor.getX(),y+1);
		else if(actor.getY()+actor.getHeight() > y+h)
			actor.setPos(actor.getX(),y+h-actor.getHeight()-1);
	
		

	}
	@Override
	public void setPos(double x, double y) 
	{
		this.x = x;
		this.y = y;
	}
	@Override
	public double getX() 
	{
		return x;
	}
	@Override
	public double getY() 
	{
		return y;
	}

}
